package com.longteng.diamond.service;

import com.google.common.collect.Collections2;
import com.longteng.diamond.dao.ProjectMapper;
import com.longteng.diamond.dao.UserMapper;
import com.longteng.diamond.domain.Project;
import com.longteng.diamond.domain.User;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.util.SessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator
 * DATE : 2018/7/4
 * TIME : 23:39
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Component
@Slf4j
public class UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    ProjectMapper projectMapper;

    @RequestMapping("/updateUser")
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    /**
     * 校验用户
     *
     * @param user
     * @return
     */
    @RequestMapping("/checkUser")
    public RequestResult checkUser(User user) {
        RequestResult requestResult = new RequestResult();
        User dbUser = userMapper.getUser(user);
        if (null == dbUser) {
            requestResult.setSuccess(false);
            requestResult.setMsg("您输入的用户名不存在!");
            return requestResult;
        }
        String password = user.getPassword();
        String dbPassword = dbUser.getPassword();
        if (!password.equalsIgnoreCase(dbPassword)) {
            requestResult.setSuccess(false);
            requestResult.setMsg("您的密码不正确!");
            return requestResult;
        }
        try {
            /**通过用户id获取有权限的项目列表**/
            List<Project> projectList = projectMapper.getProjectByUserId(dbUser.getUserId());
            /**如果是新用户,给一个默认的演示项目**/
            if (projectList.size() == 0) {
                dbUser.setLastProjectId(0);
            } else if (projectList.size() > 1) {
                Integer id = dbUser.getLastProjectId();
                if (null == id) {
                    dbUser.setLastProjectId(projectList.get(0).getProjectId());
                }
            }
            this.setInitProject(projectList);
            dbUser.setProjectList(projectList);
            requestResult.setSuccess(true);
            requestResult.setObj(dbUser);
            BeanUtils.copyProperties(dbUser, user);
        } catch (Exception e) {
            log.error("获取项目列表异常", e);
            requestResult.setSuccess(false);
            requestResult.setMsg("获取项目列表异常,请联系管理员!");
        }
        return requestResult;
    }

    /**
     * 给session的用户添加项目列表
     *
     * @param user
     * @param project
     * @return void
     */
    public void setUserProjectList(User user, Project project) {
        List<Project> projectList = user.getProjectList();
        projectList.add(project);
    }

    /**
     * 获取所有用户
     *
     * @param
     * @return long
     */
    public Collection<User> getAllUser() {
        final Integer userId = SessionUtil.getSessionUser().getUserId();
        Collection<User> result = Collections2.filter(userMapper.getAllUser(), u -> u.getUserId().intValue() != userId);
        return result;
    }

    /**
     * 为所有用户初始化项目
     *
     * @param
     * @return
     */
    private void setInitProject(List<Project> projectList) {
        Project project = new Project();
        project.setProjectId(-1);
        project.setProjectName("演示项目");
        projectList.add(project);
    }

}
